/**
 * Copyright (C), 2015-2021, XXX有限公司
 * FileName: XjfWorkOrderController
 * Author:   Administrator
 * Date:     2021/8/6 0006 11:52
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package cn.hxlc.tie.demo.modular.jf.controller;

import cn.hxlc.tie.demo.core.enums.StatusEnum;
import cn.hxlc.tie.demo.core.enums.WorkOrderEnum;
import cn.hxlc.tie.demo.modular.jf.entity.XjfWorkOrderEntry;
import cn.hxlc.tie.demo.modular.jf.model.params.CloseReasonParam;
import cn.hxlc.tie.demo.modular.jf.model.params.DeleteParam;
import cn.hxlc.tie.demo.modular.jf.model.params.XjfWorkOrderEntryParam;
import cn.hxlc.tie.demo.modular.jf.model.params.XjfWorkOrderParam;
import cn.hxlc.tie.demo.modular.jf.model.result.XjfWorkOrderEntryResult;
import cn.hxlc.tie.demo.modular.jf.model.result.XjfWorkOrderResult;
import cn.hxlc.tie.demo.modular.jf.service.XjfWorkOrderService;
import cn.hxlc.tie.libs.context.auth.LoginContext;
import cn.hxlc.tie.libs.scanner.annotation.GetResource;
import cn.hxlc.tie.libs.scanner.annotation.PostResource;
import cn.hxlc.tie.libs.scanner.stereotype.ApiResource;
import cn.hxlc.tie.model.page.PageResult;
import cn.hxlc.tie.model.web.response.ResponseData;
import com.alibaba.fastjson.JSONArray;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

import static cn.hxlc.tie.model.web.response.ResponseData.DEFAULT_SUCCESS_CODE;

/**
 * 〈一句话功能简述〉<br> 
 * 〈〉
 *
 * @author jf.xu
 * @create 2021/8/6 0006
 * @since 1.0.0
 */
@RestController
@ApiResource(name = "生产订单_jf", path = "/xuWorkOrder")
public class XjfWorkOrderController {

    @Autowired
    private XjfWorkOrderService xjfWorkOrderService;

    /**
     * 分页查询列表
     *
     * @author jf.xu
     */

    @PostResource(name = "新增", path = "/add")
    @ApiOperation("新增")
    public ResponseData xuTest1(@RequestBody XjfWorkOrderParam param) {
        System.out.print(param.toString());
        param.setFcreatorid(getCurrentUserid());
        xjfWorkOrderService.add(param);
        return ResponseData.success(ResponseData.DEFAULT_SUCCESS_CODE,"新增成功！",null);
    }

    @GetResource(name = "分页查询列表", path = "/queryListPage", requiredLogin=true, requiredPermission = false)
    @ApiOperation(value = "分页查询列表", response = XjfWorkOrderResult.class)
    public ResponseData queryListPage(XjfWorkOrderParam param) {

        PageResult<XjfWorkOrderResult> pageBySpec = xjfWorkOrderService.findPageBySpec(param);
        return ResponseData.success(pageBySpec);
    }

    @PostResource(name = "批量反审核", path = "/batchChangeZc" ,requiredPermission = false)
    @ApiOperation(value = "批量反审核" )
    public ResponseData batchChangeZc(@RequestBody List<Long> ids) {

        xjfWorkOrderService.batchChangeZc(ids);
        return ResponseData.success(DEFAULT_SUCCESS_CODE,"操作成功",null);
    }

    @PostResource(name = "批量审核", path = "/batchChangeSh" ,requiredPermission = false)
    @ApiOperation(value = "批量审核" )
    public ResponseData batchChangeSh(@RequestBody List<Long> ids) {

        xjfWorkOrderService.batchChangeSh(ids);
        return ResponseData.success(DEFAULT_SUCCESS_CODE,"操作成功",null);
    }

    @PostResource(name = "批量下达", path = "/batchChangeXd" ,requiredPermission = false)
    @ApiOperation(value = "批量下达" )
    public ResponseData batchChangeXd(@RequestBody List<Long> ids) {

        xjfWorkOrderService.batchChangeXd(ids);
        return ResponseData.success(DEFAULT_SUCCESS_CODE,"操作成功",null);
    }

    @GetResource(name = "查询详情", path = "/queryDetail", requiredLogin=true, requiredPermission = false)
    @ApiOperation(value = "查询详情", response = XjfWorkOrderResult.class)
    public ResponseData queryDetail(@RequestParam Long fid) {

        return ResponseData.success(xjfWorkOrderService.detail(fid));

    }

    @PostResource(name = "修改", path = "/update")
    @ApiOperation("修改")
    public ResponseData update(@RequestBody XjfWorkOrderParam param) {
        xjfWorkOrderService.update(param);
        return ResponseData.success(ResponseData.DEFAULT_SUCCESS_CODE,"修改成功！",null);
    }

    @PostResource(name = "关闭订单", path = "/submitCloseReason")
    @ApiOperation("关闭")
    public ResponseData submitCloseReason(@RequestBody CloseReasonParam param) {
        param.getIds();

        xjfWorkOrderService.submitCloseReason(param.getIds(), param.getCloseReason());
        return ResponseData.success(DEFAULT_SUCCESS_CODE,"操作成功",null);
    }

    @PostResource(name = "批量删除", path = "/remove")
    @ApiOperation("批量删除")
    public ResponseData remove(@RequestBody List<Long> fid) {

        xjfWorkOrderService.batchDelete(fid);
        return ResponseData.success(ResponseData.DEFAULT_SUCCESS_CODE,"删除成功！",null);
    }

    @PostResource(name = "批量删除", path = "/removeByObject")
    @ApiOperation("批量删除")
    public ResponseData removeByObject(@RequestBody DeleteParam param) {
        xjfWorkOrderService.batchDelete(param.getIds());
        return ResponseData.success(ResponseData.DEFAULT_SUCCESS_CODE,"删除成功！",null);
    }

    public static Long getCurrentUserid(){
        Long accountId = null;
        try {
            accountId = LoginContext.me().getLoginUser().getUserId();
        } catch (Exception e) {
            accountId = -1L;
        }
        return accountId;
    }
}
